using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace DynamicDNS.Provider.TCP
{
    /// <summary>
    /// Implementation for the TCP-based ChangeIP Dynamic DNS Provider
    /// </summary>
    public class ChangeIPProvider : DynamicDNS.Provider.BaseProvider.DynamicDNSTCPBaseProvider, IDynamicDNSProvider
    {
        /// <summary>
        /// This template contains the basic provider specific request.
        /// </summary>
        private const string REQUESTTEMPLATE = "GET /nic/update?" +
            "u={0}" +
            "&p={1}" +
            "&ip={2}" +
            "&hostname={3}" +
            "&offline={4}" +
            " HTTP/1.1\r\n" +
            "Host: nic.chageip.com\r\n" +
            "Connection: Close" +
            "User-Agent: DynamicDNS Library\r\n\r\n";

        public ChangeIPProvider()
        {
            ProviderDomain = "nic.changeip.com";

        }

        #region IDynamicDNSProvider Member

        public DynamicDNSUpdateResult UpdateIP(IPAddress ip, string yourdomainname, string username, string password)
        {
            return UpdateIP( ip,  yourdomainname,  username,  password,false);
        }

        public DynamicDNSUpdateResult UpdateIP(IPAddress ip, string yourdomainname, string username, string password, bool offline)
        {
            BuildRequestString(ip, yourdomainname, username, password,offline);
            return base.UpdateDNSEntry();
             
        }

        /// <summary>
        /// This is a helper method for replacing the template values against the correct values
        /// </summary>
        /// <param name="ip">Contains the IP Adress for which the Update is to processed</param>
        /// <param name="yourdomainname">Contains the Hostname for which the Update is to processed. This is Provider specific.</param>
        /// <param name="username">Contains the required username for the update</param>
        /// <param name="password">Contains the required password for the update</param>
        ///  <param name="offline">Specifies, if the host entry is to be set as offline. This is provider specific</param>
        private void BuildRequestString(IPAddress ip, string yourdomainname, string username, string password, bool offline)
        {
            Request = String.Format(REQUESTTEMPLATE, username, password, ip, yourdomainname, (offline ? "0" : "1"));
        }

        #endregion
    }
}
